package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportTableChineseEnglishRpcService;
import com.hunttown.mes.rpc.domain.TransportTableChineseEnglishDTO;
import com.hunttown.mes.rpc.domain.query.TransportTableChineseEnglishDTOQuery;
import com.hunttown.mes.service.TransportTableChineseEnglishService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportTableChineseEnglishConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:16
 */
@Service
public class TransportTableChineseEnglishRpcServiceImpl implements TransportTableChineseEnglishRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportTableChineseEnglishRpcServiceImpl.class);

    private final TransportTableChineseEnglishService transportTableChineseEnglishService;

    @Autowired
    public TransportTableChineseEnglishRpcServiceImpl(TransportTableChineseEnglishService transportTableChineseEnglishService) {
        this.transportTableChineseEnglishService = transportTableChineseEnglishService;
    }

    @Override
    public TransportTableChineseEnglishDTO insert(TransportTableChineseEnglishDTO transportTableChineseEnglishDTO) {
        try {
            return convert(transportTableChineseEnglishService.insert(convert(transportTableChineseEnglishDTO)));
        } catch (Exception e) {
            logger.error("TransportTableChineseEnglish create failed.", e);
            throw new RuntimeException("TransportTableChineseEnglish create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportTableChineseEnglishDTO transportTableChineseEnglishDTO) {
        try {
            return transportTableChineseEnglishService.updateInfoById(convert(transportTableChineseEnglishDTO));
        } catch (Exception e) {
            logger.error("TransportTableChineseEnglish update by id failed.", e);
            throw new RuntimeException("TransportTableChineseEnglish update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportTableChineseEnglishDTOQuery transportTableChineseEnglishDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportTableChineseEnglishDTOQuery.getW_id() != null && transportTableChineseEnglishDTOQuery.getW_id() > 0) {
                query.put("w_id", transportTableChineseEnglishDTOQuery.getW_id());
            }

            if (transportTableChineseEnglishDTOQuery.getIds() != null && !transportTableChineseEnglishDTOQuery.getIds().equals("")) {
                query.put("ids", transportTableChineseEnglishDTOQuery.getIds());
            }

            if (transportTableChineseEnglishDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportTableChineseEnglishDTOQuery.getStopFlag());
            }

            if (transportTableChineseEnglishDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportTableChineseEnglishDTOQuery.getDeleteFlag());
            }

            return transportTableChineseEnglishService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportTableChineseEnglish update by query failed.", e);
            throw new RuntimeException("TransportTableChineseEnglish update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTableChineseEnglishDTO selectById(Integer id) {
        try {
            return convert(transportTableChineseEnglishService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportTableChineseEnglish select by id failed.", e);
            throw new RuntimeException("TransportTableChineseEnglish select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTableChineseEnglishDTO selectObjByQuery(TransportTableChineseEnglishDTOQuery transportTableChineseEnglishDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportTableChineseEnglishDTOQuery.getTableName())) {
                query.put("tableName", transportTableChineseEnglishDTOQuery.getTableName());
            }

            if (transportTableChineseEnglishDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportTableChineseEnglishDTOQuery.getStopFlag());
            }

            if (transportTableChineseEnglishDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportTableChineseEnglishDTOQuery.getDeleteFlag());
            }

            return convert(transportTableChineseEnglishService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportTableChineseEnglish select obj by query failed.", e);
            throw new RuntimeException("TransportTableChineseEnglish select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportTableChineseEnglishDTO> getListForPage(TransportTableChineseEnglishDTOQuery transportTableChineseEnglishDTOQuery) {
        Query query = Query.page(transportTableChineseEnglishDTOQuery.getPn(), transportTableChineseEnglishDTOQuery.getPageSize());

        if (transportTableChineseEnglishDTOQuery.getId() != null && transportTableChineseEnglishDTOQuery.getId() > 0) {
            query.put("id", transportTableChineseEnglishDTOQuery.getId());
        }

        if (transportTableChineseEnglishDTOQuery.getIds() != null && !transportTableChineseEnglishDTOQuery.getIds().equals("")) {
            query.put("ids", transportTableChineseEnglishDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportTableChineseEnglishDTOQuery.getTableName())) {
            query.put("tableName", transportTableChineseEnglishDTOQuery.getTableName());
        }

        if (StringUtils.isNotBlank(transportTableChineseEnglishDTOQuery.getLikeName())) {
            query.put("likeName", transportTableChineseEnglishDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportTableChineseEnglishDTOQuery.getShowDate())) {
            query.put("showDate", transportTableChineseEnglishDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportTableChineseEnglishDTOQuery.getStartDate())) {
            query.put("startDate", transportTableChineseEnglishDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportTableChineseEnglishDTOQuery.getEndDate())) {
            query.put("endDate", transportTableChineseEnglishDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportTableChineseEnglishDTOQuery.getOrderBy())) {
            query.put("orderBy", transportTableChineseEnglishDTOQuery.getOrderBy());
        }

        if (transportTableChineseEnglishDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportTableChineseEnglishDTOQuery.getStopFlag());
        }

        if (transportTableChineseEnglishDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportTableChineseEnglishDTOQuery.getDeleteFlag());
        }

        List<TransportTableChineseEnglishDTO> list = convert(transportTableChineseEnglishService.selectByQuery(query));
        Page<TransportTableChineseEnglishDTO> page = Page.from(transportTableChineseEnglishDTOQuery.getPn(), transportTableChineseEnglishDTOQuery.getPageSize(), list);
        page.setTotalItems(transportTableChineseEnglishService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportTableChineseEnglishService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportTableChineseEnglish delete failed.", e);
            throw new RuntimeException("TransportTableChineseEnglish delete failed, Please order your code.", e);
        }
    }
}